package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.SgAccount;
import cn.jekun.component.core.service.gen.SgAccountService;
import cn.jekun.web.shop.bean.BanksAccount;
import cn.jekun.web.shop.bean.ShopMember;
import cn.jekun.web.shop.service.BanksService;


/**
 * 银行账户管理
 * 
 * @author Mr.Dong
 *
 */
@SuppressWarnings("serial")
public class BanksAction extends ShopBaseAction {
	private static final String AUTH_SHOP_MY_SECURITY="SHOP_MY_SECURITY";//安全设置
	private SgAccount sgAccount;

	public SgAccount getSgAccount() {
		return sgAccount;
	}

	public void setSgAccount(SgAccount sgAccount) {
		this.sgAccount = sgAccount;
	}

	public SgAccountService getSgAccountService() {
		return sgAccountService;
	}

	public void setSgAccountService(SgAccountService sgAccountService) {
		this.sgAccountService = sgAccountService;
	}

	private List<BanksAccount> resultBanksList = new ArrayList<BanksAccount>();
	private List<BanksAccount> resultIdCardsList = new ArrayList<BanksAccount>(); 
	public List<BanksAccount> getResultIdCardsList() {
		return resultIdCardsList;
	}

	public void setResultIdCardsList(List<BanksAccount> resultIdCardsList) {
		this.resultIdCardsList = resultIdCardsList;
	}

	public List<BanksAccount> getResultBanksList() {
		return resultBanksList;
	}

	public void setResultBanksList(List<BanksAccount> resultBanksList) {
		this.resultBanksList = resultBanksList;
	}

	@Resource(name=BanksService.BEAN_NAME)
	BanksService bankService;
	@Resource(name=SgAccountService.BEAN_NAME)
	protected SgAccountService sgAccountService;


	@SuppressWarnings("unchecked")
	public String getBankAccountsDetails(){
		//判断是否有操作权限
//		this.checkRight(AUTH_SHOP_MY_SECURITY);
//		formMap.put("shopId",this.getSessionUser().getShopId()); //当前商户ID
//		this.resultList = this.bankService.getBanksAccounts(formMap,page);
//		return "/shop/bankmanage.jsp";
		
		
		JSONObject jsonObject=new JSONObject();
		try {
			String shopId=this.getSessionUser().getShopId();//当前商户ID
			this.resultList = this.bankService.getBanksAccounts(shopId,page);

			JSONArray jsonArray=new JSONArray();

			HashMap<String, Object> hashMap;
			
			if(this.resultList.size()>0){
			
				for(int i=0;i<this.resultList.size();i++){
					
					BanksAccount tempBanksAccount=(BanksAccount) resultList.get(i);
					
					hashMap=new HashMap<String, Object>();
					
					hashMap.put("createTime",tempBanksAccount.getCreateTime()==null?"":tempBanksAccount.getCreateTime());
					
					hashMap.put("bankName",tempBanksAccount.getBankName()==null?"":tempBanksAccount.getBankName());
	
					hashMap.put("bankNumber",tempBanksAccount.getBankNumber()==null?"":tempBanksAccount.getBankNumber());
	
					hashMap.put("accountName",tempBanksAccount.getAccountName());
					
					hashMap.put("accountType",tempBanksAccount.getAccountType()==null?"":tempBanksAccount.getAccountType());
					
					hashMap.put("isAvailable",tempBanksAccount.getIsAvailable()==null?"":tempBanksAccount.getIsAvailable());
					
					hashMap.put("defaultFlag",tempBanksAccount.getDefaultFlag()==null?"":tempBanksAccount.getDefaultFlag());
					
					hashMap.put("accountId",tempBanksAccount.getAccountId()==null?"":tempBanksAccount.getAccountId());
					
					
					jsonArray.add(hashMap);
				}
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

				jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
				
				jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
				
				jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
				
				jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
							
				jsonObject.put("BankListData", jsonArray);
	
				
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, 2);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有查询到相关数据！");
				jsonObject.put("orderListData", jsonArray);
			}
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
		

	}

	//银行列表
	public String getBanksParams(){
//		try {
//			this.resultBanksList = this.bankService.getBanks(); //获取银行列表
//			//this.resultIdCardsList = this.bankService.getIdCards(); //获取证件类型列表
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return "/shop/bindingbankcard.jsp";
		
		
		String bankId=request.getParameter("bankId");
		String bankName=request.getParameter("bankName");
				
		
		JSONObject jsonObject=new JSONObject();
		try {

			this.resultBanksList = this.bankService.getBanks(bankId,bankName,page); //获取银行列表

			JSONArray jsonArray=new JSONArray();

			HashMap<String, Object> hashMap;
			
			for(int i=0;i<this.resultBanksList.size();i++){

				
				BanksAccount tempBanksAccount=(BanksAccount) resultBanksList.get(i);
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put("bankId",tempBanksAccount.getBankNumber()==null?"":tempBanksAccount.getBankNumber());
				
				hashMap.put("bankName",tempBanksAccount.getBankName()==null?"":tempBanksAccount.getBankName());
				
				
				jsonArray.add(hashMap);


			}
			jsonObject.put("banksListData", jsonArray);
			
			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}  catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 绑定银行卡
	 * @return
	 */
	public String getBindBankCard(){

		JSONObject jsonObject=new JSONObject();
		
		try {
			String accountName = request.getParameter("sgAccounts.accountName");
			String bankId = request.getParameter("sgAccounts.bankId");
			String bankNumber = request.getParameter("sgAccounts.bankNumber");
//			String identifyTypeId = request.getParameter("sgAccounts.identifyTypeId");
//			String identity = request.getParameter("sgAccounts.identity");
//			String mobile  = request.getParameter("sgAccounts.mobile");
//			String accountType  = request.getParameter("sgAccounts.accountType");
			String defaultaccount = request.getParameter("defaultaccount");
			if("true".equals(defaultaccount)){
				defaultaccount="1";
			}else{
				defaultaccount="0";
			}
			if(!this.bankService.isBanksNumber(bankNumber)){
				this.bankService.getBindBankCard(accountName, bankId, bankNumber, defaultaccount,this.getSessionUser().getShopId());
				this.logService.addShopAuditLog("", "绑定银行卡成功，银行卡号:"+bankNumber);//记录操作日志
				
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"绑定银行卡成功，银行卡号:"+bankNumber);
			}else{	
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"账号(卡号):"+bankNumber+"重复,已存在相同记录!");
			}
		}  catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 解除绑定
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String unBindBanks(){

		JSONObject jsonObject=new JSONObject();
		String accountId=request.getParameter("accountId");
		try {
			boolean isdefaultAccount  = bankService.isDefaultAccount(accountId);
			if(!isdefaultAccount){ //判断当前账户是否是唯一结算账户
				Map map = new HashMap();
				String defaultFlag="0";//是否默认为结算账户(提现标志) 1-是/0-否
				String available="0";//有效标志  1-有效/0-无效、解绑
				this.bankService.updateSgAccount(defaultFlag,available,accountId);
				
				this.logService.addShopAuditLog("", "解除绑定银行账户成功，账户ID:"+accountId);//记录操作日志
				
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"解除绑定银行账户成功，账户ID:"+accountId);
				
				
			}else{
				this.logService.addShopAuditLog("", "解除绑定银行账户失败，账户ID:"+accountId+","
						+ "当前记录为唯一结算账户");//记录操作日志
				
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"当前记录为唯一结算账户!");
			}
//			formMap.put("shopId",this.getSessionUser().getShopId());
//			this.resultList = this.bankService.getBanksAccounts(formMap,page);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		return this.writeJson(jsonObject);

	}

	/**
	 * 重新绑定
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String reBindBanks(){
		
		JSONObject jsonObject=new JSONObject();
		String accountId=request.getParameter("accountId");
		try {
			String available="1";//有效标志  1-有效/0-无效、解绑
			this.bankService.updateSgAccount(null, available, accountId);
			
			this.logService.addShopAuditLog("", "重新绑定银行账户成功，账户ID:"+accountId);//记录操作日志
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"重新绑定银行账户成功，账户ID:"+accountId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		return this.writeJson(jsonObject);
	}

	/**
	 * 设定为默认结算账户
	 * @return
	 */
	public String setDefaultAccounts(){
		JSONObject jsonObject=new JSONObject();
		
		try {
			this.bankService.setDefaultAccounts(this.getSessionUser().getShopId(),
					request.getParameter("sgAccount.accountId"));
			this.logService.addShopAuditLog("", "设置默认结算账户成功，账户ID:"
					+request.getParameter("sgAccount.accountId"));//记录操作日志
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"设置默认账户成功");
			
		}catch (Exception e) {
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		
		return this.writeJson(jsonObject);
	}

}
